package com.intelligentmeterreadingsys.waterlist.copywater.activity;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;
import com.intelligentmeterreadingsys.waterlist.activity.WaterListMainActivity;

import dbhelper.XmlNameHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CopyWaterMainActivity extends Activity implements OnClickListener{
     public Button button1;//RFID抄表
     public Button button2;//RFID异常抄表
     public Button button3;//RFID 一键上传
     public SharedPreferenceUtil sh;
     public boolean isSafe=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.copywater);
		 sh=new SharedPreferenceUtil(this);
		isSafe=sh.getBoolean(XmlNameHelper.isSafe);
		init();
	}

	/**
	 * 控件初始化
	 */
	public void init(){
		button1=(Button)findViewById(R.id.copywater_rfid_button);
		button2=(Button)findViewById(R.id.copywater_norfid_button);
		button3=(Button)findViewById(R.id.copywater_upload_button);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent=null;
		 isSafe=sh.getBoolean(XmlNameHelper.isSafe);
		
		 
		if(v.getId()==button1.getId()){
			
			 if(isSafe){
				 intent=new Intent(CopyWaterMainActivity.this, CopyWaterRFIDActivity.class);
				 startActivity(intent);
			 }else{
				 showDialog(CopyWaterRFIDActivity.class);
			 }
			
		}else if(v.getId()==button2.getId()){
			 if(isSafe){
				 intent=new Intent(CopyWaterMainActivity.this, CopyWaterNoRFIDActivity.class);
				 intent.putExtra("waterid", "");
				 startActivity(intent);
			 }else{
				 showDialog(CopyWaterNoRFIDActivity.class);
			 }
			
			
		}else if(v.getId()==button3.getId()){
		
			 if(isSafe){
				 intent=new Intent(CopyWaterMainActivity.this, CopyWaterUploadActivity.class);
				 startActivity(intent);
			 }else{
				 showDialog(CopyWaterUploadActivity.class);
			 }
		}
		
	}
	
	 public boolean showDialog(final Class<?> activityClass){
         
	  	   LayoutInflater factory = LayoutInflater.from(this);  
	         final View textEntryView = factory.inflate(R.layout.dialog, null);  
	         final EditText editTextPass = (EditText) textEntryView.findViewById(R.id.txt_password);  
	           
	         AlertDialog.Builder ad1 = new AlertDialog.Builder(this);  
	         ad1.setIcon(android.R.drawable.ic_dialog_info);  
	         ad1.setView(textEntryView);
	         ad1.setTitle("安全验证");
	         ad1.setPositiveButton("确定", new  android.content.DialogInterface.OnClickListener() {
	  		
	  		@Override
	  		public void onClick(DialogInterface dialog, int which) {
	  			String pass=editTextPass.getEditableText().toString();
	  			
	  			if(pass.equals(sh.getValue(XmlNameHelper.passWord))){
	  				isSafe=true;
	  				Intent intents=new Intent(CopyWaterMainActivity.this, activityClass);
	  				intents.putExtra("waterid", "");
	  				startActivity(intents);
	  				
	  			}else{
	  				Toast.makeText(CopyWaterMainActivity.this, "密码错误", 1000).show();
	  				isSafe=false;
	  			}
	  			
	  		}
	  	});
	         ad1.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
	  		
	  		@Override
	  		public void onClick(DialogInterface dialog, int which) {
	  		       isSafe=false;
	  			
	  		}
	  	});
	         ad1.create();
	         ad1.show();
			return isSafe;

	     }
	     

}
